const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");
// 保存派单
router.post('/receipt/lastAccount', async ctx => {
    let user = ctx.user
    let tenantId = user.tenantId;
    let buId = ctx.request.body.buId;
    let acc = await db.query(`select top 1 accId,b.name accName from 
                 invoice a inner join category b on a.tenantId=b.tenantId and a.accId=b.id
             where a.tenantId=${tenantId} and a.billType='RECEIPT' and a.buId=${buId} 
    order by a.id desc `,{type: QueryTypes.SELECT, plain: true})
    if(acc) {
        return ctx.suc('', acc)
    }
    else {
        let mod = await db.query(`select top 1 id accId,name accName from v_account where tenantId=${tenantId} order by id desc`,{type: QueryTypes.SELECT, plain: true})
        return ctx.suc('', mod)
    }

})

module.exports = router;
